package com.example.downlinetext;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;



import androidx.annotation.Nullable;

import java.util.ArrayList;


public class mDownLineText extends androidx.appcompat.widget.AppCompatTextView {

    private String mText;// 文本
    private ArrayList<String> mTextList;
    private int mTextColor;// 文本颜色
    private float mTextSize;// 文本大小
    private boolean mIsHaveTextUnderLine;// 是否有下划线
    private int mTextUnderLineColor;// 下划线颜色
    private float mTextUnderLineWidth;// 下划线宽度
    private float mTextUnderLineEndPosition;// 下划线结束位置
    private Rect mBound;// 文本范围
    private Paint mPaint;// 文字画笔
    private Paint UnderLinePaint;// 下划线画笔
    private float density;

    public mDownLineText(Context context) {
        super(context);
    }

    public mDownLineText(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);


    }




    public mDownLineText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取自定义属性的值
       init(context,attrs,defStyleAttr);
        Log.d("TAGUnderLine","mDownLineText");

    }
    private void init(Context context, AttributeSet attrs, int defStyleAttr){
        density=context.getResources().getDisplayMetrics().density;
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.mDownLineText,defStyleAttr,0);
        mText = a.getString(R.styleable.mDownLineText_mText);
        if(TextUtils.isEmpty(mText)){
            mText="";
        }
        mTextColor = a.getColor(R.styleable.mDownLineText_mTextColor, Color.BLACK);
        mTextSize = a.getDimension(R.styleable.mDownLineText_mTextSize, density);
        mIsHaveTextUnderLine = a.getBoolean(R.styleable.mDownLineText_mTextUnderLineIsHave, false);
        mTextUnderLineColor = a.getColor(R.styleable.mDownLineText_mTextUnderLineColor, Color.WHITE);
        mTextUnderLineWidth = a.getDimension(R.styleable.mDownLineText_mTextUnderLineWith, density);
        mTextUnderLineEndPosition = a.getDimension(R.styleable.mDownLineText_mTextUnderLineEndPosition, density);// 该位字符串上 中 下 半下 半上
        // 字体画笔
        a.recycle();
        mBound = new Rect();
        mPaint = new Paint();
        mPaint.setTextSize(mTextSize);
        mPaint.setColor(mTextColor);
        // 下划线画笔
        UnderLinePaint = new Paint();
        UnderLinePaint.setColor(mTextUnderLineColor);
        UnderLinePaint.setStrokeWidth(mTextUnderLineWidth);
    }


    public void setText(String text) {
        this.mText = text;
        invalidate();
    }


    public void setUnderLineColor(int color){
        this.mTextUnderLineColor = color;
        invalidate();
    }
    public void setUnderLineWidth(float width){
        this.mTextUnderLineWidth = width;
        invalidate();
    }
    @Override
    protected void onDraw(Canvas canvas) {
        if(TextUtils.isEmpty(mText)){
            int counts = getLineCount();
            for(int i=0;i<counts;i++){

            }
        }
        super.onDraw(canvas);
        Log.d("TAGUnderLine","onDraw");
    }


}
